package com.lf.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * Created by LF on 2017/5/6.
 */
@Configuration
public class MyConverterRegistry {
    @Autowired
    private ConverterRegistry converterRegistry;

    @PostConstruct
    public void init() {
        converterRegistry.addConverter(new StringToListConvert());
    }

    private static  class StringToListConvert implements Converter<String, List<String>> {
        @Override
        public List<String> convert(String source) {
            if (source == null) {
                return Arrays.asList();
            } else {
                String[] split = source.split(",");
                return Arrays.asList(split);
            }
        }
    }
}